/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:PaymentController.java
 * 模块说明：	
 * 修改历史：
 * 2017年11月14日 - liyang - 创建。
 *
 */
package com.thor.palmmall.controller.payment;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thor.palmmall.api.statement.AStatementPayBill;
import com.thor.palmmall.api.statement.StatementService;
import com.thor.palmmall.util.pay.ChinaPayUtil;

/**
 * @author:liyang
 * @time:2017年11月14日 下午2:16:02
 * @since:1.0
 */
@Controller
@RequestMapping(value = "/payment")
public class PaymentController {

  @Autowired
  StatementService statementService;

  @RequestMapping(value = "chinaPayFontCallback.thor")
  public String chinaPayFontCallback(HttpServletRequest request)
      throws UnsupportedEncodingException {
    request.setCharacterEncoding("utf-8");
    // 解析 返回报文
    Enumeration<String> requestNames = request.getParameterNames();
    Map<String, String> resultMap = new HashMap<String, String>();
    while (requestNames.hasMoreElements()) {
      String name = requestNames.nextElement();
      String value = request.getParameter(name);
      resultMap.put(name, value);
    }
    AStatementPayBill bill = statementService.getByBillNumber(resultMap.get("MerOrderNo"));
    if (bill != null) {
      // 验证签名
      if (ChinaPayUtil.verify(bill, resultMap)) {
        resultMap.put("VERIFY_KEY", "success");
      } else {
        resultMap.put("VERIFY_KEY", "fail");
      }
    }
    for (Map.Entry<String, String> entry : resultMap.entrySet()) {
      request.setAttribute(entry.getKey(), entry.getValue());
    }
    return "/payment/chinapayTips";
  }

}
